package com.cskaoyan.service.front;

import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.vo.front.ShowSearchKeywordVO;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.mapper.MarketSearchHistoryMapper;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @auther 10-何兴绕
 * @Date 2022年10月15日 09:46
 */
@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    MarketKeywordMapper keywordMapper;

    @Autowired
    MarketSearchHistoryMapper searchHistoryMapper;

    @Override
    public ShowSearchKeywordVO showSearchKeyword() {

        Integer sortOrderMax = keywordMapper.selectSortOrderMax();
        keywordMapper.updateDefaultZero();
        keywordMapper.updateDefault(sortOrderMax, new Date());

        keywordMapper.updateHotValue();
        MarketKeywordExample hotSearchExample = new MarketKeywordExample();
        PageHelper.startPage(1, 10);
        hotSearchExample.setOrderByClause("sort_order desc");
        List<MarketKeyword> hotSearchKeywords = keywordMapper.selectByExample(hotSearchExample);
        for (MarketKeyword hotKey : hotSearchKeywords) {
            hotKey.setIsHot(true);
            keywordMapper.updateByPrimaryKeySelective(hotKey);
        }

        MarketKeywordExample defaultKeywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria defaultKeywordCriteria = defaultKeywordExample.createCriteria();
        defaultKeywordCriteria.andIsDefaultEqualTo(true);
        defaultKeywordCriteria.andDeletedEqualTo(false);
        List<MarketKeyword> defaultKeywords = keywordMapper.selectByExample(defaultKeywordExample);
        MarketKeyword defaultKeyword = defaultKeywords.get(0);

        MarketKeywordExample hotKeywordExample = new MarketKeywordExample();
        hotKeywordExample.setOrderByClause("sort_order desc");
        MarketKeywordExample.Criteria keywordCriteria = hotKeywordExample.createCriteria();
        keywordCriteria.andIsHotEqualTo(true);
        keywordCriteria.andDeletedEqualTo(false);
        List<MarketKeyword> hotKeywordList = keywordMapper.selectByExample(hotKeywordExample);

        MarketSearchHistoryExample historyExample = new MarketSearchHistoryExample();
        List<MarketSearchHistory> searchHistories = null;
        try {
            MarketUser primaryPrincipal = (MarketUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
            MarketSearchHistoryExample.Criteria historyCriteria = historyExample.createCriteria();
            historyCriteria.andUserIdEqualTo(primaryPrincipal.getId());
            historyCriteria.andDeletedEqualTo(false);
            historyExample.setOrderByClause("update_time desc");
            searchHistories = searchHistoryMapper.selectByExample(historyExample);
        } catch (NullPointerException e) {
            e.getMessage();
        }

        ShowSearchKeywordVO showSearchKeywordVO = new ShowSearchKeywordVO();
        showSearchKeywordVO.setDefaultKeyword(defaultKeyword);
        showSearchKeywordVO.setHotKeywordList(hotKeywordList);
        showSearchKeywordVO.setHistoryKeywordList(searchHistories);
        return showSearchKeywordVO;
    }

    @Override
    public List<String> searchKeys(String key) {

        List<String> keywords = new ArrayList<>();
        MarketKeywordExample example = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = example.createCriteria();
        criteria.andKeywordLike("%" + key + "%");
        criteria.andDeletedEqualTo(false);
        List<MarketKeyword> marketKeywords = keywordMapper.selectByExample(example);
        for (MarketKeyword keyword : marketKeywords) {
            keywords.add(keyword.getKeyword());
        }

        return keywords;
    }

    @Override
    public int clearHistory() {
        searchHistoryMapper.updateHistory();
        return 200;
    }
}
